/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.config;

import org.openislands.oi.constant.MessageCodeEnum;
import org.openislands.oi.error.LogicException;
import org.openislands.oi.pojo.vo.CommonResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.client.RestClientException;

@SuppressWarnings("unused")
@RestControllerAdvice
public class GlobalExceptionCatch {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionCatch.class);

    @ExceptionHandler(Throwable.class)
    public CommonResponse<Void> globalExceptionCatch(Throwable e) {
        log.error("global error:", e);
        return CommonResponse.error(MessageCodeEnum.SYSTEM_ERROR);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public CommonResponse<Void> argumentValidCatch(MethodArgumentNotValidException e) {
        log.error("parameter error", e);
        final FieldError fieldError = e.getBindingResult().getFieldError();
        if (fieldError == null) return CommonResponse.error(MessageCodeEnum.PARAMETER_ERROR);
        String errorMessage = String.format("%s parameter %s %s",
                fieldError.getObjectName(), fieldError.getField(), fieldError.getDefaultMessage());
        return CommonResponse.error(errorMessage, MessageCodeEnum.PARAMETER_ERROR);
    }

    @ExceptionHandler(RestClientException.class)
    public CommonResponse<Void> restClientCatch(RestClientException e) {
        log.error("invoke error", e);
        final CommonResponse<Void> response = CommonResponse.error(MessageCodeEnum.CALL_CONNECT_ERROR);
        response.setErrorMessage(e.getLocalizedMessage());
        return response;
    }

    @ExceptionHandler(LogicException.class)
    public CommonResponse<Void> logicCatch(LogicException e) {
        log.error("logic error:", e);
        final CommonResponse<Void> response = CommonResponse.error(e.getResponseMessageCode());
        response.setErrorMessage(e.getLocalizedMessage());
        return response;
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public CommonResponse<Void> illegalArgumentCatch(IllegalArgumentException e) {
        log.error("illegal argument error:", e);
        return CommonResponse.error(e.getMessage(), MessageCodeEnum.PARAMETER_ERROR);
    }
}
